﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TY_Hoist.WinApp.Code;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using TY_WinApp.Wrapper;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;

namespace TY_Hoist.WinApp
{
    public partial class FlowForm : Form
    {
        public FlowForm()
        {
            InitializeComponent();

          
        }
        double trackBarSetup = 0;
        int allFrameCount = 0;
        double currentFrame = 0;
        int startFrame = 0;
        int endFrame = 0;
        int startTime = 0;
        int endTime = 0;
        private void FlowForm_Load(object sender, EventArgs e)
        {
            if (StaticData.TestMujuType == 1)
            {
                this.panel2.Location = new Point(447, 410);
            }
            else
            {
                this.panel2.Location = new Point(12, 410);
            }
            this.panel2.Width = (int)(StaticData.TestJuli * 17.43);
            this.groupBox3.Text = StaticData.TestJuli + " cm";

            MsgTipBox.ShowDialog("正在打开视频中...", new Action(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    allFrameCount = CVVideo.Open(System.Windows.Forms.Application.StartupPath + @"\CamVideo.avi");
                    trackBarSetup = allFrameCount / 1000.0;
                    this.trackBar1.Value = 1;
                    SetImageFrame(1);

                }));
            }));
            InitSet();
        }

        private void groupBox3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox3.BackColor);
            SizeF fontSize = e.Graphics.MeasureString(groupBox3.Text, groupBox3.Font);
            e.Graphics.DrawString(groupBox3.Text, groupBox3.Font, Brushes.MediumBlue, (groupBox3.Width - fontSize.Width) / 2, 1);
            e.Graphics.DrawLine(Pens.MediumBlue, 1, 10, (groupBox3.Width - fontSize.Width) / 2, 10);
            e.Graphics.DrawLine(Pens.MediumBlue, (groupBox3.Width + fontSize.Width) / 2 - 4, 10, groupBox3.Width - 2, 10);
        }
        public void SetImageFrame(double num)
        {
            currentFrame = num;
            CVVideo.setPictureBoxControlImage(pictureBox1, (int)currentFrame);
            label7.Text = ((int)currentFrame).ToString();

        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (videoAutoPlayFlag == false)
            {
                SetImageFrame((int)(trackBar1.Value * trackBarSetup));
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            videoAutoPlayFlag = true;
            var setFrameIndex = currentFrame - (int)this.numericUpDown1.Value;
            setFrameIndex = setFrameIndex < 1 ? 1 : setFrameIndex;
            SetImageFrame(setFrameIndex);
            this.trackBar1.Value = (int)(setFrameIndex / trackBarSetup);
            videoAutoPlayFlag = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            videoAutoPlayFlag = true;
            var setFrameIndex = currentFrame + (int)this.numericUpDown1.Value;
            setFrameIndex = setFrameIndex > allFrameCount ? allFrameCount : setFrameIndex;
            SetImageFrame(setFrameIndex);
            this.trackBar1.Value = (int)(setFrameIndex / trackBarSetup);
            videoAutoPlayFlag = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "播放")
            {
                StartVideoAutoPlay();
            }
            else
            {
                StopVideoAutoPlay();
            }
        }

        bool videoAutoPlayFlag = false;
        public void StartVideoAutoPlay()
        {
            button3.Text = "停止";
            button4.Enabled = button5.Enabled = false;
            videoAutoPlayFlag = true;
            Task.Factory.StartNew(() =>
            {
                while (videoAutoPlayFlag)
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        var setFrameIndex = currentFrame + (double)(numericUpDown2.Value/100);
                        if (setFrameIndex > allFrameCount)
                        {
                            StopVideoAutoPlay();
                        }
                        setFrameIndex = setFrameIndex > allFrameCount ? allFrameCount : setFrameIndex;
                        SetImageFrame(setFrameIndex);
                        this.trackBar1.Value = (int)(setFrameIndex / trackBarSetup);

                    }));

                    Thread.Sleep(10);
                }


            });

        }

        public void StopVideoAutoPlay()
        {
            button3.Text = "播放";
            button4.Enabled = button5.Enabled = true;
            videoAutoPlayFlag = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            startFrame = (int)currentFrame;
            StaticData.StartFrame = startFrame;
            label3.Text = startFrame.ToString();
            label12.Text = (startFrame / 1000.0).ToString() + "秒";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            endFrame = (int)currentFrame;
            StaticData.EndFrame = endFrame;
            label4.Text = endFrame.ToString();
            label13.Text = (endFrame / 1000.0).ToString() + "秒";
        }

        public void InitSet()
        {
            startFrame = StaticData.StartFrame;
            endFrame = StaticData.EndFrame;
            label3.Text = startFrame.ToString();
            label12.Text = (startFrame / 1000.0).ToString() + "秒";
            label4.Text = endFrame.ToString();
            label13.Text = (endFrame / 1000.0).ToString() + "秒";

            currentFrame = startFrame;
            SetImageFrame(currentFrame);
            videoAutoPlayFlag = true;
            this.trackBar1.Value = (int)(currentFrame / trackBarSetup);
            videoAutoPlayFlag = false;
        }
    }
}
